home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -serious- / misc / pdflib / util / compile_metrics.c next >
C/C++ Source or Header  |  1999-12-06  |  7KB  |  219 lines

  1. /*---------------------------------------------------------------------------*
  2.  |        PDFlib - A library for dynamically generating PDF files            |
  3.  +---------------------------------------------------------------------------+
  4.  |        Copyright (c) 1997-1999 Thomas Merz. All rights reserved.          |
  5.  +---------------------------------------------------------------------------+
  6.  |    This software is not in the public domain.  It is subject to the       |
  7.  |    "Aladdin Free Public License".  See the file license.txt for details.  |
  8.  |    This license grants you the right to use and redistribute PDFlib       |
  9.  |    under certain conditions. Among other things, the license requires     |
  10.  |    that the copyright notice and this notice be preserved on all copies.  |
  11.  |    This requirement extends to ports to other programming languages.      |
  12.  |                                                                           |
  13.  |    In short, you are allowed to develop and use PDFlib-based software     |
  14.  |    as long as you don't sell it. Commercial use of PDFlib requires a      |
  15.  |    commercial license which can be obtained from the author of PDFlib.    |
  16.  |    Contact information can be found in the accompanying PDFlib manual.    |
  17.  |    PDFlib is distributed with no warranty of any kind. Commercial users,  |
  18.  |    however, will receive warranty and support statements in writing.      |
  19.  *---------------------------------------------------------------------------*/
  20.  
  21. /* compileafm.c
  22.  *
  23.  * Generate C header file with font metrics
  24.  *
  25.  */
  26.  
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <ctype.h>
  30.  
  31. #ifndef WIN32
  32. #include <unistd.h>
  33. #endif
  34.  
  35. #ifdef NeXT
  36. #include <libc.h>    /* for getopt(), optind, optarg */
  37. #endif
  38.  
  39. #ifdef __CYGWIN32__
  40. #include <getopt.h>    /* for getopt(), optind, optarg */
  41. #endif
  42.  
  43. #include "p_intern.h"
  44. #include "p_afmparse.h"
  45.  
  46. #include "ansi_e.h"
  47. #include "macrom_e.h"
  48. #include "pdfdoc_e.h"
  49.  
  50. #ifdef WIN32
  51. int getopt(int nargc, char **nargv, char *ostr);
  52. char *optarg;
  53. int optind;
  54. #endif
  55.  
  56. const char *pdf_encoding_enum_names[] = {
  57.     "builtin", "pdfdoc", "macroman", "macexpert", "winansi"
  58. };
  59.  
  60. pdf_encodingvector *pdf_encodings[] = {
  61.     NULL, &pdf_pdfdoc, &pdf_macroman, NULL, &pdf_winansi
  62. };
  63.  
  64. void
  65. add_entry(PDF *p, FILE *out, pdf_font font, pdf_encoding enc)
  66. {
  67.     int        i;
  68.  
  69.     fprintf(out, 
  70.         "\n/* ---------------------------------------------------------- */\n");
  71.     fprintf(out, "{");
  72.     fprintf(out, "\t\"%s\",\t\t\t/* FontName */\n", font.name);
  73.     fprintf(out, "\t%s,\t\t\t/* Encoding */\n",
  74.         pdf_encoding_enum_names[font.encoding]);
  75.     fprintf(out, "\t0,\t\t\t\t/* used on current page */\n");
  76.     fprintf(out, "\t0,\t\t\t\t/* embed flag */\n");
  77.     fprintf(out, "\t(char *) NULL,\t\t\t/* Name of external font file */\n");
  78.     fprintf(out, "\t0L,\t\t\t\t/* Object id */\n");
  79.     fprintf(out, "\t%ul,\t\t\t/* Font flags */\n", font.flags);
  80.     fprintf(out, "\tpdf_false,\t\t\t/* Read from AFM file */\n");
  81.     fprintf(out, "\t\"%s\",\t\t\t/* Full name */\n", font.fullName);
  82.     fprintf(out, "\t\"%s\",\t\t/* Native encoding */\n", font.encodingScheme);
  83.     fprintf(out, "\t\"%s\",\t\t\t/* Family name */\n", font.familyName);
  84.     fprintf(out, "\t\"%s\",\t\t\t\t/* Weight */\n", font.weight);
  85.  
  86.     fprintf(out, "\t(float) %2.1f,\t\t\t/* ItalicAngle */\n", font.italicAngle);
  87.     fprintf(out, "\t%d,\t\t\t\t/* isFixedPitch */\n", font.isFixedPitch);
  88.     fprintf(out, "\t%d,\t\t\t\t/* llx */\n", font.llx);
  89.     fprintf(out, "\t%d,\t\t\t\t/* lly */\n", font.lly);
  90.     fprintf(out, "\t%d,\t\t\t\t/* urx */\n", font.urx);
  91.     fprintf(out, "\t%d,\t\t\t\t/* ury */\n", font.ury);
  92.     fprintf(out, "\t%d,\t\t\t\t/* UnderlinePosition */\n", font.underlinePosition);
  93.     fprintf(out, "\t%d,\t\t\t\t/* UnderlineThickness */\n",font.underlineThickness);
  94.     fprintf(out, "\t%d,\t\t\t\t/* CapHeight */\n", font.capHeight);
  95.     fprintf(out, "\t%d,\t\t\t\t/* xHeight */\n", font.xHeight);
  96.     fprintf(out, "\t%d,\t\t\t\t/* Ascender */\n", font.ascender);
  97.     fprintf(out, "\t%d,\t\t\t\t/* Descender */\n", font.descender);
  98.     fprintf(out, "\t%d,\t\t\t\t/* StdVW */\n", font.StdVW);
  99.     fprintf(out, "\t%d,\t\t\t\t/* StdHW */\n", font.StdHW);
  100.  
  101.     fprintf(out, "\n\t0,\t\t\t\t/* numOfChars */\n");
  102.     fprintf(out, "\t(CharMetricInfo *) NULL,\t/* cmi */\n");
  103.     fprintf(out, "\t0,\t\t\t\t/* numOfTracks */\n");
  104.     fprintf(out, "\t(TrackKernData *) NULL,\t\t/* tkd */\n");
  105.     fprintf(out, "\t0,\t\t\t\t/* numOfPairs */\n");
  106.     fprintf(out, "\t(PairKernData *) NULL,\t\t/* pkd */\n");
  107.     fprintf(out, "\t0,\t\t\t\t/* numOfComps */\n");
  108.     fprintf(out, "\t(CompCharData *) NULL,\t\t/* ccd */\n");
  109.  
  110.     fprintf(out, "\n/* Character metrics for font %s */\n", font.name);
  111.     fprintf(out, "{");
  112.  
  113.     for (i=0; i < 256; i++) {
  114.     fprintf(out, "\t%3d", font.widths[i]);
  115.     if (i == 255)
  116.         fprintf(out, " ");
  117.     else
  118.         fprintf(out, ",");
  119.  
  120.     if (i % 8 == 7)
  121.         fprintf(out, " /* 0x%02x */\n", i);
  122.     }
  123.  
  124.     fprintf(out, "}\n");
  125.     fprintf(out, "},\n");
  126.  
  127.     /* free AFM parser's storage */
  128.     (void) pdf_cleanup_afm(p, &font);
  129. }
  130.  
  131. int
  132. main(int argc, char *argv[])
  133. {
  134.     int   opt;
  135.     char *filename, *outfilename = NULL;
  136.     pdf_encoding enc;
  137.     FILE *out;
  138.     PDF  *p;
  139.     pdf_font font;
  140.     size_t len;
  141.  
  142.     /* This is only a dummy to provide a PDF* for the auxiliary functions */
  143.     if ((p = PDF_new()) == NULL) {
  144.     fprintf(stderr, "Couldn't generate internal PDF object - aborting\n");
  145.     exit(99);
  146.     }
  147.  
  148.     /* By default, generate compiled metrics for the current platform.
  149.      * This may also be changed since the encoding is recorded in the
  150.      * generated C structs. */
  151. #ifdef MAC
  152.     enc = macroman;
  153. #else
  154.     enc = winansi;
  155. #endif
  156.  
  157.     while ((opt = getopt(argc, argv, "e:o:")) != -1)
  158.     switch (opt) {
  159.         case 'e':
  160.         if (!strcmp(optarg, "macroman"))
  161.             enc = macroman;
  162.         else if (!strcmp(optarg, "winansi"))
  163.             enc = winansi;
  164.         else if (!strcmp(optarg, "pdfdoc"))
  165.             enc = pdfdoc;
  166.         else {
  167.             fprintf(stderr, "Encoding %s not supported!\n", optarg);
  168.             exit(88);
  169.         }
  170.         break;
  171.  
  172.         case 'o':
  173.             outfilename = optarg;
  174.         if ((out = fopen(outfilename, "w")) == NULL) {
  175.             fprintf(stderr, "Couldn't open output file %s!\n",
  176.                 outfilename);
  177.             exit(99);
  178.         }
  179.  
  180.         break;
  181.  
  182.         default:
  183.             break;
  184.     }
  185.  
  186.     if (outfilename == NULL) {
  187.     fprintf(stderr, "%s: Compile AFM font metrics to C code\n", argv[0]);
  188.     fprintf(stderr, 
  189.         "Usage: %s -e [winansi|macroman|pdfdoc] -o outfile afmfiles...\n",
  190.         argv[0]);
  191.     exit(1);
  192.     }
  193.  
  194.     while (optind < argc) {
  195.     filename = argv[optind++];
  196.     fprintf(stderr, "Adding %s\n", filename);
  197.  
  198.     /* parse PFM file */
  199.     len = strlen(filename);
  200.     if (len >= 5 && !strcmp(filename + len - 4, ".pfm")) {
  201.         if (!pdf_get_metrics_pfm(p, &font, NULL,  enc, filename)) {
  202.         fprintf(stderr, "Error parsing AFM file '%s' - skipped!\n",
  203.         filename);
  204.         } else
  205.         add_entry(p, out, font, enc);
  206.     } else {
  207.         /* parse AFM file */
  208.         if (!pdf_get_metrics_afm(p, &font, NULL,  enc, filename)) {
  209.         fprintf(stderr, "Error parsing AFM file '%s' - skipped!\n",
  210.         filename);
  211.         } else
  212.         add_entry(p, out, font, enc);
  213.     }
  214.     }
  215.  
  216.     fclose(out);
  217.     exit(0);
  218. }
  219.